#include "hv/hv.h"

#include <future>
#include <chrono>
#include <iostream>

bool is_prime(int x) {
    std::this_thread::sleep_for (std::chrono::milliseconds(5000));
    for (int i = 2; i < x / 2; ++i) if (x % i == 0) return false;
    return true;
}

void print_ten(char c, int ms) {
    for (int i = 0; i < 10; ++i) {
        std::this_thread::sleep_for (std::chrono::milliseconds(ms));
        std::cout << c << std::flush;
    }
}

int main(int argc, char *argv[]) {
    logger_set_handler(hlog, stdout_logger);
    logger_enable_color(hlog, 1);

    /*
    LOGI("with launch::async:");
    std::future<void> foo = std::async(std::launch::async, print_ten, '*', 100);
    std::future<void> bar = std::async(std::launch::async, print_ten, '@', 200);
    */

    LOGI("with launch::deferred:");
    std::future<void> foo = std::async(std::launch::deferred, print_ten, '*', 100);
    std::future<void> bar = std::async(std::launch::deferred, print_ten, '@', 200);

    foo.get();
    bar.get();

    std::cout << std::endl;

    LOGI("Finished.");

    /*
    std::future<bool> f = std::async(is_prime, 1303284983294);
    LOGI("checking...");

    std::chrono::milliseconds span(100);

    while (f.wait_for(span) == std::future_status::timeout)
        std::cout << '.' << std::flush;

    bool x = f.get();
    std::cout << std::endl;
    LOGI("ret: %d", x);
    */

    return 0;
}
